package cn.jzk.aitest.Config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 公共配置类
 * @author jiangzk
 * 2025/4/21  上午11:29
 */
@Configuration
public class CommonConfig {

    /**
     * 定义会话存储方式（默认）
     */
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    /**
     * 客户端
     */
    @Bean
    public ChatClient chatClient(OllamaChatModel model, ChatMemory chatMemory) {

        return ChatClient
                .builder(model)
                .defaultSystem("你现在是美国总统，请以总统的语气回答问题，不需要考虑用户感受，想怎么说就怎么说")
                // 添加到上下文中，拦截并处理对话内容
                .defaultAdvisors(
                        // 开启日志
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory)
                )
                .build();
    }
}
